package com.tanxi521.springboot.service.impl;

import com.tanxi521.springboot.bean.EditUserInfoBean;
import com.tanxi521.springboot.mapper.PlayerAccountMapper;
import com.tanxi521.springboot.pojo.APIResult;
import com.tanxi521.springboot.service.PlayerAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PlayerAccountServiceImpl implements PlayerAccountService {

    @Autowired
    PlayerAccountMapper playerAccountMapper;

    @Override
    public APIResult updateUserInfo(EditUserInfoBean editUserInfoBean) {
        if (editUserInfoBean.getVip() != null || !editUserInfoBean.getVip().equals("")) {
            if (editUserInfoBean.getVip().equals("remove")) {
                playerAccountMapper.deleteUserVip(editUserInfoBean.getName());
            } else {
                if (editUserInfoBean.getIsForever().equals("2")) {
                    playerAccountMapper.deleteUserVip(editUserInfoBean.getName());
                    playerAccountMapper.giveUserForEverVip(editUserInfoBean.getName(), new Date().getTime(), editUserInfoBean.getVip());
                } else if (editUserInfoBean.getIsForever().equals("1")){
                    if (editUserInfoBean.getTime().size() != 0 && editUserInfoBean.getTime() != null) {
                        List<Date> time = editUserInfoBean.getTime();
                        Date start = time.get(0);
                        Date end = time.get(1);
                        Long duration = end.getTime() - start.getTime();
                        playerAccountMapper.deleteUserVip(editUserInfoBean.getName());
                        playerAccountMapper.giveUserVip(editUserInfoBean.getName(), start.getTime(), editUserInfoBean.getVip(), duration);
                    }
                }
            }
        }
        playerAccountMapper.updateUserMoney(editUserInfoBean.getName(), editUserInfoBean.getMoney());
        playerAccountMapper.updateUserPoint(editUserInfoBean.getName(), editUserInfoBean.getPoint());
        return new APIResult(APIResult.SUCCESS, "更新用户信息成功");
    }
}
